﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MM.Model;
using MM.DBUtility;

namespace MM.DAL
{
    public class T_Roles
    {
        public bool Exists(int RoleID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_Roles");
            strSql.Append(" where RoleID=@RoleID ");
            SqlParameter[] parameters = {
					new SqlParameter("@RoleID", SqlDbType.Int)};
            parameters[0].Value = RoleID;

            return DbHelperSql.Exists(strSql.ToString(), parameters);
        }

        public void Add(MM.Model.T_Roles model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_Roles(");
            strSql.Append("RoleName,Powers,Remark)");
            strSql.Append(" values (");
            strSql.Append("@RoleName,@Powers,@Remark)");
            SqlParameter[] parameters = {
					new SqlParameter("@RoleName", SqlDbType.VarChar,50),
					new SqlParameter("@Powers", SqlDbType.VarChar,100),
					new SqlParameter("@Remark", SqlDbType.VarChar,500)};
            parameters[0].Value = model.RoleName;
            parameters[1].Value = model.Powers;
            parameters[2].Value = model.Remark;

            DbHelperSql.ExecuteSql(strSql.ToString(), parameters);
        }

        public void Update(MM.Model.T_Roles model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_Roles set ");
            strSql.Append("RoleName=@RoleName,");
            strSql.Append("Powers=@Powers,");
            strSql.Append("Remark=@Remark,");
            strSql.Append(" where RoleID=@RoleID ");

            SqlParameter[] parameters = {
					new SqlParameter("@RoleID", SqlDbType.Int),
					new SqlParameter("@RoleName", SqlDbType.VarChar,50),
					new SqlParameter("@Powers", SqlDbType.VarChar,100),
					new SqlParameter("@Remark", SqlDbType.VarChar,500)};
            parameters[0].Value = model.RoleID;
            parameters[1].Value = model.RoleName;
            parameters[2].Value = model.Powers;
            parameters[3].Value = model.Remark;

            DbHelperSql.ExecuteSql(strSql.ToString(), parameters);
        }

        public void Delete(int RoleID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_Roles ");
            strSql.Append(" where RoleID=@RoleID ");
            SqlParameter[] parameters = {
					new SqlParameter("@RoleID", SqlDbType.Int)};
            parameters[0].Value = RoleID;

            DbHelperSql.ExecuteSql(strSql.ToString(), parameters);
        }
    }
}
